/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.CTPhieuMuaHangDTO;

/**
 *
 * @author tuan
 */
public class CTPhieuMuaHangDAO {

    public CTPhieuMuaHangDAO() {
    }
    
    public ArrayList<CTPhieuMuaHangDTO> GetCTPhieuMuaHangByMaPMH(String MaPMH) throws SQLException {
        ArrayList<CTPhieuMuaHangDTO> listCT = new ArrayList<>();
        Object[] objs = { MaPMH };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetCTPhieuMHByMaPhieuMHFromDatabase( ? )", objs);
        CTPhieuMuaHangDTO ctPhieuMH;
        while (result.next()) {
            ctPhieuMH = new CTPhieuMuaHangDTO(result.getString("MAPHIEUMH"), result.getString("MASP"), 
                    result.getInt("SOLUONG"), result.getLong("THANHTIEN"));
            listCT.add(ctPhieuMH);
        }
        return listCT;
    }
    
    public ArrayList<CTPhieuMuaHangDTO> GetCTPhieuMuaHangByMaSanPham(String MaSP) throws SQLException {
        ArrayList<CTPhieuMuaHangDTO> listCT = new ArrayList<>();
        Object[] objs = { MaSP };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetCTPhieuMHByMaSPFromDatabase( ? )", objs);
        CTPhieuMuaHangDTO ctPhieuMH;
        while (result.next()) {
            ctPhieuMH = new CTPhieuMuaHangDTO(result.getString("MAPHIEUMH"), result.getString("MASP"), 
                    result.getInt("SOLUONG"), result.getLong("THANHTIEN"));
            listCT.add(ctPhieuMH);
        }
        return listCT;
    }
    
    public int UpdateCTPhieuMH(CTPhieuMuaHangDTO ctPhieuMH) {
        Object[] objs = { ctPhieuMH.getMaPhieuMH(), ctPhieuMH.getMaSP(), ctPhieuMH.getSoLuong(), 
                    ctPhieuMH.getThanhTien()};
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdateCTPhieuMuaHangToDatabase(?, ? ,?, ?)", objs);
    }
    
    public int DeleteCTPhieuMH(CTPhieuMuaHangDTO ctPhieuMH) {
        Object[] objs = { ctPhieuMH.getMaPhieuMH(), ctPhieuMH.getMaSP() };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteChiTietPMHToDatabase(?, ?)", objs);
    }
    
    public int InsertCTPhieuMH(CTPhieuMuaHangDTO ctPhieuMH) {
        Object[] objs = { ctPhieuMH.getMaPhieuMH(), ctPhieuMH.getMaSP(), ctPhieuMH.getSoLuong(), 
                    ctPhieuMH.getThanhTien()};
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertChiTietPMHToDatabaseW(?, ? ,?, ?)", objs);
    }
}
